package service

import (
	"chaowan/data_source"
	"chaowan/models"
	"math/rand"
	"time"
)

func Gooduser(user models.User, good models.Goods, num, state, status, usergoodareaid, orderid int64) {
	now := time.Now()
	zeroToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()

	usergoodadd := models.Usergood{
		Uid:            user.ID,
		State:          state,
		Status:         status,
		Addtime:        zeroToday,
		Goodid:         good.ID,
		Goodname:       good.Name,
		Goodimage:      good.Image,
		Goodprice:      good.Price,
		Goodmojing:     good.Mojing,
		Usergoodareaid: usergoodareaid,
		Num:            1,
		Orderid:        orderid,
		Shouhuo:        3,
	}
	data_source.Db.Create(&usergoodadd)

	return

}

type ChoujiangBody struct {
	Goodid int64 `json:"goodid" gorm:"goodid"`
	Bili   int   `json:"bili" gorm:"bili"`
	Dengji int64 `json:"dengji" gorm:"dengji"`
}
type Prize struct {
	name   string
	weight int
}

func GetResult(niudangood []models.Niudangood) (goodid, dengji int64) {
	var totalWeight int
	var prizes []ChoujiangBody
	for _, v := range niudangood {
		prize := ChoujiangBody{
			Goodid: v.Goodid,
			Bili:   int(v.Bili),
			Dengji: v.Classify,
		}
		prizes = append(prizes, prize)
		totalWeight += int(v.Bili)
	}
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	randNum := r.Intn(totalWeight)
	var current int
	for _, prize := range prizes {
		current += prize.Bili
		if current >= randNum {
			return prize.Goodid, prize.Dengji
		}
	}
	return -1, -1
}

func GetResultfs(fanshanggood []models.Fanshanggood) (goodid, dengji int64) {
	var totalWeight int
	var prizes []ChoujiangBody
	for _, v := range fanshanggood {
		prize := ChoujiangBody{
			Goodid: v.Goodid,
			Bili:   int(v.Bili),
			Dengji: v.Classify,
		}
		prizes = append(prizes, prize)
		totalWeight += int(v.Bili)
	}
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	randNum := r.Intn(totalWeight)
	var current int
	for _, prize := range prizes {
		current += prize.Bili
		if current >= randNum {
			return prize.Goodid, prize.Dengji
		}
	}
	return -1, -1
}
func CalculateProbability(probability int64) bool {
	// 初始化随机数种子
	rand.Seed(time.Now().UnixNano())
	// 生成一个1000以内的随机数
	randomNumber := rand.Intn(1000)
	// 判断随机数是否小于中奖概率
	return int64(randomNumber) < probability
}

//
//func Choujiang(prizes []models.Niudangood, num int) (data []int64) {
//	//prizes := []int{10, 20, 30, 40, 50} // 假设每个奖项的中奖概率是其数值的比例
//
//	var total float64                  // 计算总概率
//	probabilities := []ChoujiangBody{} // 存储每个奖项的累计概率
//
//	// 计算总概率和累计概率数组
//	for _, prize := range prizes {
//		total += prize.Bili
//
//		probabilitie := ChoujiangBody{
//			Goodid: prize.Goodid,
//			Bili:   prize.Bili,
//		}
//		probabilities = append(probabilities, probabilitie)
//	}
//
//	// 设置随机数种子
//	rand.Seed(time.Now().UnixNano())
//	var goodidarr []int64
//	// 模拟抽奖过程
//	for i := 0; i < num; i++ { // 假设抽取10次
//		number := rand.Intn(int(total)) // 在[0, total)范围内生成一个随机数
//		fmt.Println(number)
//		for _, probability := range probabilities {
//			if number < int(probability.Bili) {
//				fmt.Println(probability.Bili)
//				fmt.Println(probability.Goodid)
//				return
//				goodidarr = append(goodidarr, probability.Goodid)
//				fmt.Println(goodidarr)
//				break
//			}
//
//		}
//	}
//	return goodidarr
//}
